﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utils
{
    /// <summary>
    /// 页面索引类
    /// </summary>
    public class PageIndex
    {
        //开始索引
        private long startIndex;
        //结束索引
        private long endIndex;

        #region
        public long StartIndex
        {
            get { return startIndex; }
            set { startIndex = value; }
        }

        public long EndIndex
        {
            get { return endIndex; }
            set { endIndex = value; }
        }
        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="startIndex">开始索引</param>
        /// <param name="endIndex">结束索引</param>
        public PageIndex(long startIndex, long endIndex)
        {
            this.startIndex = startIndex;
            this.endIndex = endIndex;
        }

        /// <summary>
        /// 获取页面索引
        /// </summary>
        /// <param name="viewPageCount"> 每页显示页码数量</param>
        /// <param name="currentPage">当前页码</param>
        /// <param name="totalPage">总页数</param>
        /// <returns>获取页面索引</returns>
        public static PageIndex GetPageIndex(long viewPageCount, int currentPage, long totalPage)
        {
            //开始页码
            long startPage = currentPage - (viewPageCount % 2 == 0 ? viewPageCount / 2 - 1 : viewPageCount / 2);
            //结束页码
            long endPage = currentPage + viewPageCount / 2;

            //如果开始页码小于1
            if (startPage < 1)
            {
                //将开始页码置为1
                startPage = 1;
                //如果总页数大于等于每页显示的页码数量 ,结束页码为每页显示页码数量
                if (totalPage >= viewPageCount) endPage = viewPageCount;
                    //否则结束页等于总页数
                else endPage = totalPage; 
            }
            if (endPage > totalPage)
            {
                endPage = totalPage;
                if ((endPage - viewPageCount) > 0) startPage = endPage - viewPageCount + 1;
                else startPage = 1;
            }
            return new PageIndex(startPage, endPage);
        }

    }
}
